package com.yunxi.service.dubboService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.otd.OtdModelProduceConfig;
import com.yunxi.otd.pojo.OtdModelProduceConfigQueryVO;
import com.yunxi.otd.pojo.OtdModelProduceConfigVO;
import com.yunxi.otd.service.OtdModelProduceConfigDubboService;
import com.yunxi.service.localService.otd.OtdModelProduceConfigService;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * <p>
 * 车型投产日设置 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-07-09
 */
public class OtdModelProduceConfigDubboServiceImpl implements OtdModelProduceConfigDubboService {

    @Autowired
    @Qualifier(value = "otdModelProduceConfigServiceImpl")
    OtdModelProduceConfigService otdModelProduceConfigService;

    @Override
    public ApiResult saveOrUpdate(OtdModelProduceConfigVO configVO, LoginInfo info) {
        OtdModelProduceConfig config = convertBean(configVO);
        LambdaQueryWrapper<OtdModelProduceConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OtdModelProduceConfig::getRuleExpression, configVO.getRuleExpression());
        // 校验规则是否已维护
        if (!StringUtil.isEmpty(config.getId())) {
            queryWrapper.ne(OtdModelProduceConfig::getId, config.getId());
        }
        int count = otdModelProduceConfigService.count(queryWrapper);
        if (count > 0) {
            return ResponseUtil.fail(-1, "规则：" + configVO.getRuleExpression() + "已维护");
        }
        otdModelProduceConfigService.saveOrUpdate(config);
        return ResponseUtil.ok();
    }

    @Override
    public OtdModelProduceConfigVO getById(String id) {
        return convertVO(otdModelProduceConfigService.getById(id));
    }

    @Override
    public void removeByIds(List<String> ids) {
        otdModelProduceConfigService.removeByIds(ids);
    }

    @Override
    public PageInfo findPageByVO(OtdModelProduceConfigQueryVO vo, Integer pageNum, Integer pageSize) {
        return otdModelProduceConfigService.pageByVo(vo, pageNum, pageSize);
    }

    @Override
    public ApiResult export(OtdModelProduceConfigQueryVO queryVO) {
        return ResponseUtil.ok(otdModelProduceConfigService.listByVo(queryVO));
    }

    /**
     * VO to BEAN
     *
     * @param configVO VO
     * @return BEAN
     */
    private OtdModelProduceConfig convertBean(OtdModelProduceConfigVO configVO) {
        OtdModelProduceConfig config = new OtdModelProduceConfig();
        BeanUtils.copyProperties(configVO, config);
        return config;
    }

    /**
     * BEAN to VO
     *
     * @param config BEAN
     * @return VO
     */
    private OtdModelProduceConfigVO convertVO(OtdModelProduceConfig config) {
        OtdModelProduceConfigVO configVO = new OtdModelProduceConfigVO();
        BeanUtils.copyProperties(config, configVO);
        return configVO;
    }
}
